package cn.wei.youvie.utils;

import java.io.*;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

/**
 * word文档工具类
 */
public class WordUtils {
    public static void main(String[] args) {

        File file = new File("C:\\Users\\Youvie\\Desktop\\www.docx");
        try {
            WordUtils.readDocx(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 读取docx格式的word文档的内容
     */
    public static List<XWPFParagraph> readDocx(InputStream fis) throws FileNotFoundException {
        try (XWPFDocument document = new XWPFDocument(fis)) {
            return document.getParagraphs();

        } catch (IOException e) {
            throw new FileNotFoundException("文件读取异常，请检查文件格式！");
        }
    }

    /**
     * 读取doc格式的word文档的全文
     */
    public static String readDocContent(InputStream fis) throws FileNotFoundException {
        try (HWPFDocument document = new HWPFDocument(fis);
             WordExtractor extractor = new WordExtractor(document)) {

            return extractor.getText();
        } catch (IOException e) {
            throw new FileNotFoundException("文件读取异常，请检查文件格式！");
        }
    }
}
